package org.jivesoftware.smack.proxy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.SocketFactory;
import org.jivesoftware.smack.util.Base64;

class HTTPProxySocketFactory extends SocketFactory
{
  private static final Pattern RESPONSE_PATTERN = Pattern.compile("HTTP/\\S+\\s(\\d+)\\s(.*)\\s*");
  private ProxyInfo proxy;

  public HTTPProxySocketFactory(ProxyInfo paramProxyInfo)
  {
    this.proxy = paramProxyInfo;
  }

  private Socket httpProxifiedSocket(String paramString, int paramInt)
    throws IOException
  {
    String str5;
    StringBuilder localStringBuilder;
    int j;
    int k;
    String str1 = this.proxy.getProxyAddress();
    int i = this.proxy.getProxyPort();
    Socket localSocket = new Socket(str1, i);
    String str2 = "CONNECT " + paramString + ":" + paramInt;
    String str3 = this.proxy.getProxyUsername();
    if (str3 == null)
      str5 = "";
    while (true)
    {
      localSocket.getOutputStream().write(str2 + " HTTP/1.1\r\nHost: " + str2 + str5 + "\r\n\r\n".getBytes("UTF-8"));
      InputStream localInputStream = localSocket.getInputStream();
      localStringBuilder = new StringBuilder(100);
      j = 0;
      k = (char)localInputStream.read();
      localStringBuilder.append(k);
      if (localStringBuilder.length() <= 1024)
        break;
      throw new ProxyException(ProxyInfo.ProxyType.HTTP, "Recieved header of >1024 characters from " + str1 + ", cancelling connection");
      String str4 = this.proxy.getProxyPassword();
      str5 = "\r\nProxy-Authorization: Basic " + new String(Base64.encodeBytes(new StringBuilder(String.valueOf(str3)).append(":").append(str4).toString().getBytes("UTF-8")));
    }
    if (k == -1)
      throw new ProxyException(ProxyInfo.ProxyType.HTTP);
    if ((((j == 0) || (j == 2))) && (k == 13))
      ++j;
    while (true)
    {
      while (true)
      {
        if (j != 4);
        if (j == 4)
          break label385;
        throw new ProxyException(ProxyInfo.ProxyType.HTTP, "Never received blank line from " + str1 + ", cancelling connection");
        if (((j != 1) && (j != 3)) || (k != 10))
          break;
        ++j;
      }
      j = 0;
    }
    label385: String str6 = localStringBuilder.toString();
    StringReader localStringReader = new StringReader(str6);
    BufferedReader localBufferedReader = new BufferedReader(localStringReader);
    String str7 = localBufferedReader.readLine();
    if (str7 == null)
      throw new ProxyException(ProxyInfo.ProxyType.HTTP, "Empty proxy response from " + str1 + ", cancelling");
    Matcher localMatcher = RESPONSE_PATTERN.matcher(str7);
    if (!(localMatcher.matches()))
      throw new ProxyException(ProxyInfo.ProxyType.HTTP, "Unexpected proxy response from " + str1 + ": " + str7);
    if (Integer.parseInt(localMatcher.group(1)) != 200)
      throw new ProxyException(ProxyInfo.ProxyType.HTTP);
    return localSocket;
  }

  public Socket createSocket(String paramString, int paramInt)
    throws IOException, UnknownHostException
  {
    return httpProxifiedSocket(paramString, paramInt);
  }

  public Socket createSocket(String paramString, int paramInt1, InetAddress paramInetAddress, int paramInt2)
    throws IOException, UnknownHostException
  {
    return httpProxifiedSocket(paramString, paramInt1);
  }

  public Socket createSocket(InetAddress paramInetAddress, int paramInt)
    throws IOException
  {
    return httpProxifiedSocket(paramInetAddress.getHostAddress(), paramInt);
  }

  public Socket createSocket(InetAddress paramInetAddress1, int paramInt1, InetAddress paramInetAddress2, int paramInt2)
    throws IOException
  {
    return httpProxifiedSocket(paramInetAddress1.getHostAddress(), paramInt1);
  }
}